package com.play.ballen.synthetic.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import com.github.customview.MyEditText
import com.github.customview.MyTextView
import com.hjq.toast.ToastUtils
import com.play.ballen.R
import com.yuven.appframework.widget.SimpleTextWatcher
import com.yuven.baselib.utils.Logger

class GuessDialogUtils (private val mContext: Context) {

    val dialog: Dialog = Dialog(mContext)
    private lateinit var contentView: View
    private lateinit var cb0: CheckBox
    private lateinit var cb1: CheckBox
    private lateinit var cb2: CheckBox
    private lateinit var cb3: CheckBox
    private lateinit var cb4: CheckBox
    private lateinit var cb5: CheckBox
    private lateinit var cb6: CheckBox
    private lateinit var cb7: CheckBox
    private lateinit var cb8: CheckBox
    private lateinit var cb9: CheckBox
    private lateinit var tvAmount: TextView


    var numberCallback: ((String) -> Unit)? = null

    private val selectedNumbers: MutableSet<Int> = mutableSetOf()

    private val mInputMethodManager by lazy {
        mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    }

    private var rate: Float = 0f    //汇率

    private var rgClearing: Boolean = false

    private var nums: Int = 0

    private var mPrice: Float = 0f
    // 手续费 1BMT   汇率    当前客户的总金额
    fun show(handlingFee: Float, rateValue: Float, totalAmount: Float,price:Float) {
        this.rate = rateValue
        this.mPrice = price
        if (!this::contentView.isInitialized) {
            contentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_synthetic_guess, null)
            contentView.let {
                //rgNum = it.findViewById(R.id.rgNum)
                tvAmount = it.findViewById(R.id.tvAmount)
                cb0 = it.findViewById(R.id.cb0)
                cb1 = it.findViewById(R.id.cb1)
                cb2 = it.findViewById(R.id.cb2)
                cb3 = it.findViewById(R.id.cb3)
                cb4 = it.findViewById(R.id.cb4)
                cb5 = it.findViewById(R.id.cb5)
                cb6 = it.findViewById(R.id.cb6)
                cb7 = it.findViewById(R.id.cb7)
                cb8 = it.findViewById(R.id.cb8)
                cb9 = it.findViewById(R.id.cb9)
                cb0.isChecked = true
                selectedNumbers.add(0)
                cb0.setOnCheckedChangeListener { _, isChecked ->
                    handleCheckBoxSelection(0, isChecked)
                }

                cb1.setOnCheckedChangeListener { _, isChecked ->
                    handleCheckBoxSelection(1, isChecked)
                }

                cb2.setOnCheckedChangeListener { _, isChecked ->
                    handleCheckBoxSelection(2, isChecked)
                }

                cb3.setOnCheckedChangeListener { _, isChecked ->
                    handleCheckBoxSelection(3, isChecked)
                }

                cb4.setOnCheckedChangeListener { _, isChecked ->
                    handleCheckBoxSelection(4, isChecked)
                }

                cb5.setOnCheckedChangeListener { _, isChecked ->
                    handleCheckBoxSelection(5, isChecked)
                }

                cb6.setOnCheckedChangeListener { _, isChecked ->
                    handleCheckBoxSelection(6, isChecked)
                }

                cb7.setOnCheckedChangeListener { _, isChecked ->
                    handleCheckBoxSelection(7, isChecked)
                }

                cb8.setOnCheckedChangeListener { _, isChecked ->
                    handleCheckBoxSelection(8, isChecked)
                }

                cb9.setOnCheckedChangeListener { _, isChecked ->
                    handleCheckBoxSelection(9, isChecked)
                }

                it.findViewById<TextView>(R.id.btnBack).setOnClickListener { btn ->
                    dialog.dismiss()
                }

                it.findViewById<ImageView>(R.id.imgClose).setOnClickListener { btn ->
                    dialog.dismiss()
                }

                it.findViewById<TextView>(R.id.btnOk).setOnClickListener { btn ->
                    dialog.dismiss()
                    if (selectedNumbers.isNotEmpty()) {
                        val selectedNumbersString = selectedNumbers.joinToString(",")
                        numberCallback?.invoke(selectedNumbersString)
                    }
                }

                dialog.setContentView(it)
                dialog.setCanceledOnTouchOutside(false)
                dialog.window?.setLayout(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT
                )
                dialog.window?.setBackgroundDrawable(ColorDrawable())
                dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
                dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE or WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
            }
            tvAmount.text = price.toString()
//            tvAmount.addTextChangedListener(object : SimpleTextWatcher() {
//                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
//                    val msg: String = tvAmount.text.toString().trim { it <= ' ' }
//                    if (TextUtils.isEmpty(msg)) {
//                        return
//                    }
//                    val num = msg.toInt()
//                    val total = (handlingFee + num) / rate
//
//                    tvAmount.setSelection(msg.length)
//                }
//            })
        }

        if (!dialog.isShowing) {
            dialog.show()

            tvAmount.post {
                tvAmount.requestFocus()
                mInputMethodManager.showSoftInput(tvAmount, InputMethodManager.SHOW_IMPLICIT)
            }
        }
    }

    private fun handleCheckBoxSelection(number: Int, isChecked: Boolean) {
        if (isChecked) {
            selectedNumbers.add(number)
        } else {
            selectedNumbers.remove(number)
        }
        val selectedNums = selectedNumbers.size
        val totalPrice = mPrice * selectedNums

        // Update the tvAmount TextView with the new total price
        tvAmount.text = String.format("%.8f", totalPrice)


    }


}